package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<ContactData> cachedContacts;

	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;
	}
	
	private void rebuildCache() {
		cachedContacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();

		List<WebElement> checkboxes = driver.findElements(By.xpath("//input[@name='selected[]']"));
		List<WebElement> firstnames = driver.findElements(By.xpath("//tbody//td[3]"));
		List<WebElement> lastnames = driver.findElements(By.xpath("//tbody//td[2]"));
		List<WebElement> homephones = driver.findElements(By.xpath("//tbody//td[5]"));

		
		for (int index = 0; index < checkboxes.size(); index++) {
			ContactData contact = new ContactData();

			contact.firstname = firstnames.get(index).getText();
			contact.lastname = lastnames.get(index).getText();
			contact.homephone = homephones.get(index).getText();

			cachedContacts.add(contact);
		}
	}

	
	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().mainPage();
		gotoAddContactPage();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		rebuildCache();
		return this;
	}

	public ContactHelper modifyContact(int index, ContactData contact) {
		initContactModification(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		rebuildCache();
		return this;
	}

	public ContactHelper removeContact(int index) {
		initContactModification(index);
		submitContactRemoval();
		rebuildCache();
		return this;
	}
	
	public ContactHelper gotoAddContactPage() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.firstname);
		type(By.name("lastname"), contact.lastname);
		type(By.name("address"), contact.address);
		type(By.name("home"), contact.homephone);
		type(By.name("mobile"), contact.mobilephone);
		type(By.name("work"), contact.workphone);
		type(By.name("email"), contact.email);
		type(By.name("email2"), contact.email2);
		selectByText(By.name("bday"), contact.bday);
		selectByText(By.name("bmonth"), contact.bmonth);
		type(By.name("byear"), contact.byear);
		if (formType == CREATION){
		// selectByText(By.name("new_group"), contact.group);
		} else {
			if (driver.findElements(By.name("new_group")).size() != 0 ){
				throw new Error("Group selector exists in contact modification form");
			}
		}
		type(By.name("address2"), contact.address2);
		type(By.name("phone2"), contact.phone2);
		return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}

	public ContactHelper returnToHomePage() {
		click(By.linkText("home_page"));
		return this;
	}

	public ContactHelper initContactModification(int index) {
		index++;
		click(By.xpath("(//a[contains(@href,'edit.php?id=')])[" + index + "]"));
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//input[@value='Update']"));
		cachedContacts = null;
		return this;
	}

	public void submitContactRemoval() {
		click(By.xpath("//input[@value='Delete']"));
		cachedContacts = null;
	}

}
